﻿var eventId;
var eventDate;
var trackId;
var eventName;
$(function () {
    $('#colorpicker').colorPicker();
    $("#colorPicker_hex-0").val('#333399');
    $('#txtTimeStart').timepicker();
    $('#txtTimeEnd').timepicker();


    eventId = querySt('id');

    ajaxCall('../DataService.asmx/GetEventbyId', false, '{"id" : "' + eventId + '"}',
        function (msg) {
            eventName = msg.d.EventName;
            $('.lblEventName').html(eventName);
        },
        function () {
            alert("Error happend while reading data from database");
        }
    );

    getAllTracks();

    $("#btnCreateTrack").click(function () {
        $('#insertTrackDialog').dialog({
            width: 350,
            modal: true,
            closeOnEscape: false
        });
    });

    $('#btnCreateTimeSlots').click(function () {
        ajaxCall('../DataService.asmx/GetEventbyId', false, '{"id" : "' + eventId + '"}',
                    function (msg) {
                        $('#lblEventDate').html(JSONDate(msg.d.StartingFrom));
                        eventDate = JSONDateToNormal(msg.d.StartingFrom);
                        $('#lblStartsAt').html(JSONTime(msg.d.StartingFrom));
                    },
                    function () {
                        alert("Error while reading data from database");
                    }
                );

        $('#splitInTimeSlotsDialog').dialog({
            width: 350,
            modal: true,
            closeOnEscape: false
        });
    });

    $('#btnCreateTimeSlots').click(function () {
        getTimeSlots();
    });

    $('#showSlots').click(function () {
        if ($('#unavailableTimeSlots').css("display") == "none") {
            $('#unavailableTimeSlots').css("display", "inherit");
            $('#showSlots').html("Hide created time slots");
        } else {
            $('#unavailableTimeSlots').css("display", "none");
            $('#showSlots').html("Show created time slots");
        }
    });

});

function getAllTracks() {
    $('#Tracks').empty();
    ajaxCall('../DataService.asmx/GetAllTracks', false, '{"eventId" : "' + eventId + '"}',
                function (msg) {
                    if (msg.d == null) {
                        $('#Tracks').html("There are no tracks created for this event!");
                    }
                    else {
                        for (i in msg.d) {
                            var tracks = [
                                { trackID: msg.d[i].TrackID, trackName: msg.d[i].TrackName, trackColor: msg.d[i].TrackColor, description: msg.d[i].Description }
                            ];
                           $('#TrackTemplate').tmpl(tracks).appendTo('#Tracks');
                            getSessionsInTrack(msg.d[i].TrackID);
                        }
                    }
                },
                function () {
                    alert("Error in reading from database");
                }
            );
}

function getSessionsInTrack(id) {
    ajaxCall('../DataService.asmx/GetSessionsInTrack', false, '{"trackId":"' + id + '", "eventId":"' + eventId + '"}',
                function (msg) {
                    if (msg.d == null) {
                        $('#' + id).html("Sessions not found!");
                    } else {
                        for (i in msg.d) {
                            var speakers = msg.d[i].Speakers[0].FirstName + " " + msg.d[i].Speakers[0].LastName;
                            for (var j = 1; j < msg.d[i].Speakers.length; j++) {
                                speakers = speakers + " & " + msg.d[i].Speakers[j].FirstName + " " + msg.d[i].Speakers[j].LastName;
                            }
                            var sessions = [
                                { ID: msg.d[i].ID, Title: msg.d[i].Title, Speakers: speakers, From: JSONTime(msg.d[i].TimeSlot.From), To: JSONTime(msg.d[i].TimeSlot.To) }
                                ];

                            $('#SessionTemplate').tmpl(sessions).appendTo('#' + id);
                        }
                    }
                },
                function () {
                    alert("Error happend while reading data");
                }
            );
    }


function getTimeSlots() {
    ajaxCall('../DataService.asmx/GetTimeSlotsForEvent', false, '{"eventId": "' + eventId + '"}',
                function (msg) {
                    $('#unavailableTimeSlots').empty();
                    if (msg.d == null) {
                        $('#unavailableTimeSlots').html("TimeSlots not found!");
                    }
                    else {
                        for (i in msg.d) {
                            $('#unavailableTimeSlots').append("<button type='button' onclick='DeleteSlot(\"" + msg.d[i].ID + "\");'>Del</button> From <strong>" + JSONTime(msg.d[i].From) + "</strong> To <strong>" + JSONTime(msg.d[i].To) + "</strong> " + msg.d[i].Type);
                            $('#unavailableTimeSlots').append("<br />");
                        }
                    }
                },
                function () {
                    alert("Error happend while reading data from database");
                }
            );

}


function getTimeSlotsTemplate() {
    ajaxCall('../DataService.asmx/GetTimeSlotsForTrack', false, '{"trackId": "' + trackId + '", "eventId": "' + eventId + '" }',
                function (msg) {
                    if (msg.d == null) {
                        $('#TimeSlotsForEvent').html("TimeSlots not found!");
                    } else {
                        for (i in msg.d) {
                            if (msg.d[i].Type == 'Presentation') {
                                var timeSlots = [
                                    { ID: msg.d[i].ID, from: JSONTime(msg.d[i].From), to: JSONTime(msg.d[i].To) }
                                ];
                                $('#TimeSlotTemplate').tmpl(timeSlots).appendTo('#TimeSlotsForEvent');
                            }
                        }
                    }
                },
                function () {
                    alert("Error happend while reading data from database");
                }
     );

}


function InsertTrack() {
    $("#insertTrackValidation").validate({
        rules: {
            trackname: "required",
            trackcolor: "required"
        },
        messages: {
            trackname: "Track name is required",
            trackcolor: "Track color is required"
        },
        errorPlacement: function (error, element) {
            error.insertBefore(element);
        }
    });
    
    if ($('#txtTrackName').valid() && $('#txtTrackColor').valid()) {
        var dataToSend = '{"trackName":"' + $('#txtTrackName').val() + '", "trackColor":"' + $('#colorpicker').val() + '", "description":"' + $('#txtDescription').val() + '", "eventId": "' + eventId + '"}';
        ajaxCall('../DataService.asmx/InsertTrack', false, dataToSend,
                    function () {
                        $('#txtTrackName').val("");
                        $('#txtTrackColor').val("");
                        $('#txtDescription').val("");
                        $('#insertTrackDialog').dialog("close");
                        getAllTracks();
                    },
                    function () {
                        alert("Error happend while inserting data to database");
                    }

       );

    }
}

function InsertTimeSlot() {
    $('#timeSlotsValidation').validate({
        rules: {
            timestart: "required",
            timeend: "required"
        },
        messages: {
            timestart: "Starting time is required",
            timeend: "Ending time is required"
        },

        errorPlacement: function (error, element) {
            error.insertBefore(element);
        }
 });
 
    if ($('#txtTimeStart').valid() && $('#txtTimeEnd').valid()) {
        var dataToSend = '{"from":"' + eventDate + ' ' + $('#txtTimeStart').val() + '", "to":"' + eventDate + ' ' + $('#txtTimeEnd').val() + '", "eventId": "' + eventId + '", "type": "'+$('#ddlType option:selected').val()+'"}';
        ajaxCall('../DataService.asmx/InsertTimeSlot', false, dataToSend,
                    function () {
                        $('#txtTimeStart').val(" ");
                        $('#txtTimeEnd').val(" ");
                        getTimeSlots();
                    },
                    function () {
                        alert("Error happend while inserting data to database");
                    }
               );

    }
}


function DeleteSlot(id) {
    ajaxCall('../DataService.asmx/DeleteTimeSlot', false, '{"id":"' + id + '"}',
                function () {
                    getTimeSlots();
                },
                function () {
                    alert("Error happend while deleting this item");
                }
            );

}

function InsertSessionInTrack(id) {
    trackId = id;
    $('#SessionsForEvent').empty();
    $('#TimeSlotsForEvent').empty();
    GetAllSessionsNotInTrack();
    getTimeSlotsTemplate();
    $('#InsertSessionDialog').dialog({
        width: 600,
        modal: true,
        resizable: false
    });
}

function GetAllSessionsNotInTrack() {
    ajaxCall('../DataService.asmx/GetSessionsNotInTrack', false, '{"trackId":"' + trackId + '", "eventId":"' + eventId + '"}',
                function (msg) {
                    if (msg.d == null) {
                        $('#SessionsForEvent').html("Sessions not found!");
                    }
                    else {
                        for (i in msg.d) {
                            var speakers = msg.d[i].Speakers[0].FirstName + " " + msg.d[i].Speakers[0].LastName;
                            for (var j = 1; j < msg.d[i].Speakers.length; j++) {
                                speakers = speakers + ", " + msg.d[i].Speakers[j].FirstName + " " + msg.d[i].Speakers[j].LastName;
                            }
                            var sessions = [
                            { ID: msg.d[i].ID, Title: msg.d[i].Title, Speakers: speakers }
                            ];

                            $('#MiniSessionTemplate').tmpl(sessions).appendTo('#SessionsForEvent');
                        }
                    }
                },
                function () {
                    alert("Error happend while reading data from database");
                }
            );
}

function changeColor() {
    $('#txtTrackColor').val($('#colorpicker').val());
}

function InsertSessionTrackTime() {

    var sessionId = "";
    var timeSlotId = "";
    $('input:radio[name=rbSession]').each(function () {
        if ($(this).is(':checked')) {
            sessionId = $(this).val();
        }
    });

    $('input:radio[name=rbTimeSlot]').each(function () {
        if ($(this).is(':checked')) {
            timeSlotId = $(this).val();
        }
    });


    if (sessionId == "" || timeSlotId == "") {
        $("#lblSessionInTrackError").css("display", "inherit");
    }
    else {
        $("#lblSessionInTrackError").css("display", "none");
        var data = '{"sessionId":"' + sessionId + '", "timeSlotId":"' + timeSlotId + '", "trackId":"' + trackId + '"}';
        ajaxCall('../DataService.asmx/InsertSessionInTimeSlotInTrack', false, data,
                    function () {
                        $('#SessionsForEvent').empty();
                        $('#TimeSlotsForEvent').empty();
                        GetAllSessionsNotInTrack();
                        getTimeSlotsTemplate();
                        getAllTracks();
                    },
                    function () {
                        alert("Error happend while inserting data in the database");
                    }
                );

    }

}

function DeleteTrack(id) {
    if (confirm("Are you sure that you want to delete this item?")) {
        ajaxCall('../DataService.asmx/DeleteTrack', false, '{"trackId":"' + id + '"}',
                    function () {
                        alert("Deleting was successful");
                        getAllTracks();
                    },
                    function () {
                        alert("Error happend while deleting this item!");
                    }
                );

    }
}

function DeleteSessionFromTrack(id) {
    if (confirm("Are you sure that you want to delete this item?")) {
        ajaxCall('../DataService.asmx/DeleteSessionFromTrack', false, '{"id":"' + id + '"}',
                    function () {
                        alert("Deleting was successful");
                        getAllTracks();
                        
                    },
                    function () {
                        alert("Error happend while deleting this item!");
                    }
                );
    }
}